<?php
/* 预约记录相关操作控制器 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class ReservationRecordController extends HomeController
{
    // 预约状态 /* 1=待签到，2=待审核 ，3=已完成 ，4=已取消，5=临时闭馆 */
    public  $status = [1 => '待签到', 2 => '待审核', 3 => '已完成', 4 => '已取消', 5 => '临时闭馆'];
    // 是否需要讲解
    public  $is_explain = [0 => '否', 1 => '是'];


    //获取个人预约记录列表
    public function personal_list(Request $request)
    {
        try {
            // 分页参数
            $page = $request->input('page', 1);
            $limit = $request->input('limit', 10);
            $date = $request->input('date');
            $status = $request->input('status');

            $condition = [];
            if ($date) {
                //把日期分割成数组
                $arr = explode('~', $date);
                $condition[] = ['date', '>=', trim($arr[0])];
                $condition[] = ['date', '<=', trim($arr[1])];
            }
            if ($status) {
                $condition[] = ['status', '=', $status];
            }

            $field = ['id', 'date', 'start_time', 'end_time', 'user_id', 'num', 'status'];

            // 查询预约记录列表
            $data = DB::table('reservation_personal')->where($condition)->select($field)->orderBy('created_at', 'desc')->offset(($page - 1) * $limit)->limit($limit)->get()->toArray();


            // 统计总记录数
            $total = DB::table('reservation_personal')->count();

            //获取预约人id
            $user_ids = array_column($data, 'user_id');
            //根据预约人id查询预约人姓名
            $user_name = DB::table('visitor_user')->whereIn('id', $user_ids)->pluck('name', 'id');

            //把开始时间和结束时间拼接起来
            foreach ($data as $key => $value) {
                $data[$key]->user_name = $user_name[$value->user_id];
                $data[$key]->time = $value->start_time . ' - ' .$value->end_time;
                $data[$key]->status_name = $this->status[$value->status];
            }

            return response()->json([
                'code' => HTTP_OK,
                'message' => '获取个人预约记录列表成功',
                'data' => $data,
                'count' => $total,
            ]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('获取个人预约记录列表失败: ' . $e->getMessage());

            return response()->json(['code' => HTTP_ERROR,'message' => '获取个人预约记录列表失败','data' => null]);
        }
    }

    //获取个人预约记录详情
    public function personal_detail(Request $request)
    {
        try {
            // 分页参数
            $id = $request->input('id');

            // 根据id查询预约记录
            $data = DB::table('reservation_personal')->where('id', $id)->first();
           
            //根据预约人id查询预约人姓名
            $data->user_name = DB::table('visitor_user')->where('id', $data->user_id)->value('name');
            //整理时间和状态
            $data->time = $data->start_time . ' - ' .$data->end_time;
            $data->status_name = $this->status[$data->status];

            //查询该预约记录下的参观者信息
            $visitor = DB::table('reservation_personal_visitor')->where('reservation_id', $id)->get()->toArray();
            foreach ($visitor as $key => $value) {
                $visitor[$key]->ID_type_name = $this->ID_type[$value->ID_type];
            }
            $data->visitor = $visitor;

            return response()->json([
                'code' => HTTP_OK,
                'message' => '获取个人预约记录详情成功',
                'data' => $data,
            ]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('获取个人预约记录详情失败: ' . $e->getMessage());
            return response()->json(['code' => HTTP_ERROR,'message' => '获取个人预约记录详情失败','data' => null]);
        }
    }


    //获取个人预约记录列表
    public function team_list(Request $request)
    {
        try {
            // 分页参数
            $page = $request->input('page', 1);
            $limit = $request->input('limit', 10);
            $date = $request->input('date');
            $status = $request->input('status');
            $is_explain = $request->input('is_explain');

            $condition = [];
            //判断查询条件
            if ($date) {
                //把日期分割成数组
                $arr = explode('~', $date);
                $condition[] = ['date', '>=', trim($arr[0])];
                $condition[] = ['date', '<=', trim($arr[1])];
            }
            if ($status) {
                $condition[] = ['status', '=', $status];
            }
            if ($is_explain || $is_explain !== NULL) {
                $condition[] = ['is_explain', '=', $is_explain];
            }

            $field = ['id', 'date', 'start_time', 'end_time', 'team_name', 'unit_name', 'num', 'contacts', 'phone', 'is_explain', 'status'];

            // 查询预约记录列表
            $data = DB::table('reservation_team')->where($condition)->select($field)->orderBy('created_at', 'desc')->offset(($page - 1) * $limit)->limit($limit)->get()->toArray();

            // 统计总记录数
            $total = DB::table('reservation_team')->count();

            //把开始时间和结束时间拼接起来
            foreach ($data as $key => $value) {
                $data[$key]->time = $value->start_time . ' - ' .$value->end_time;
                $data[$key]->status_name = $this->status[$value->status];
                $data[$key]->is_explain_name = $this->is_explain[$value->is_explain];
            }

            return response()->json([
                'code' => HTTP_OK,
                'message' => '获取团队预约记录列表成功',
                'data' => $data,
                'count' => $total,
            ]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('获取团队预约记录列表失败: ' . $e->getMessage());
            return response()->json(['code' => HTTP_ERROR,'message' => '获取团队预约记录列表失败','data' => null]);
        }
    }

    //获取个人预约记录详情
    public function team_detail(Request $request)
    {
        try {
            // 分页参数
            $id = $request->input('id');

            // 根据id查询团队预约记录
            $data = DB::table('reservation_team')->where('id', $id)->first();
           
            //根据预约人id查询预约人姓名
            $data->user_name = DB::table('visitor_user')->where('id', $data->user_id)->value('name');
            //整理时间和状态
            $data->time = $data->start_time . ' - ' .$data->end_time;
            $data->status_name = $this->status[$data->status];
            $data->is_explain_name = $this->is_explain[$data->is_explain];

            //查询该预约记录下的参观者信息
            $visitor = DB::table('reservation_team_visitor')->where('reservation_id', $id)->get()->toArray();
            foreach ($visitor as $key => $value) {
                $visitor[$key]->ID_type_name = $this->ID_type[$value->ID_type];
            }
            $data->visitor = $visitor;

            return response()->json([
                'code' => HTTP_OK,
                'message' => '获取团队预约记录详情成功',
                'data' => $data,
            ]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('获取团队预约记录详情失败: ' . $e->getMessage());
            return response()->json(['code' => HTTP_ERROR,'message' => '获取团队预约记录详情失败','data' => null]);
        }
    }



    //获取活动申请列表
    public function activity_list(Request $request)
    {
        try {
            // 分页参数
            $page = $request->input('page', 1);
            $limit = $request->input('limit', 10);
            $date = $request->input('date');
            $status = $request->input('status');

            $condition = [];
            if ($date) {
                //把日期分割成数组
                $arr = explode('~', $date);
                $condition[] = ['date', '>=', trim($arr[0])];
                $condition[] = ['date', '<=', trim($arr[1])];
            }
            if ($status) {
                $condition[] = ['status', '=', $status];
            }

            $field = ['id', 'time', 'name', 'num', 'activity_area_name', 'activity_duration', 'unit_name', 'contacts', 'phone', 'status'];

            // 查询预约记录列表
            $data = DB::table('activity_application')->where($condition)->select($field)->orderBy('created_at', 'desc')->offset(($page - 1) * $limit)->limit($limit)->get()->toArray();

            // 统计总记录数
            $total = DB::table('activity_application')->count();

            //把开始时间和结束时间拼接起来
            foreach ($data as $key => $value) {
                $data[$key]->status_name = $this->status[$value->status];
            }

            return response()->json([
                'code' => HTTP_OK,
                'message' => '获取个人预约记录列表成功',
                'data' => $data,
                'count' => $total,
            ]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('获取个人预约记录列表失败: ' . $e->getMessage());

            return response()->json(['code' => HTTP_ERROR,'message' => '获取个人预约记录列表失败','data' => null]);
        }
    }

    //获取活动申请详情
    public function activity_detail(Request $request)
    {
        try {
            // 分页参数
            $id = $request->input('id');
            //1是，0否，2未选择
            $parameter = [0 => '否', 1 => '是'];

            // 根据id查询详情
            $data = DB::table('activity_application')->where('id', $id)->first();
           
            //根据预约人id查询预约人姓名
            $data->user_name = DB::table('visitor_user')->where('id', $data->user_id)->value('name');
            //整理数据
            $data->status_name = $this->status[$data->status];
            $data->is_placing_items_name = $parameter[$data->is_placing_items] ?? '';
            $data->is_live_name = $parameter[$data->is_live] ?? '';
            $data->is_video_recording_name = $parameter[$data->is_video_recording] ?? '';
            $data->is_open_name = $parameter[$data->is_open] ?? '';

            return response()->json([
                'code' => HTTP_OK,
                'message' => '获取活动申请详情成功',
                'data' => $data,
            ]);
        } catch (\Exception $e) {
            // 记录错误日志
            Log::error('获取活动申请详情失败: ' . $e->getMessage());
            return response()->json(['code' => HTTP_ERROR,'message' => '获取活动申请详情失败','data' => null]);
        }
    }

    

}
